package com.shuqing.download.engine.distribute.dispatch.assemble.strategy.easy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;

import com.shuqing.download.engine.distribute.dispatch.assemble.AssembleContext;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.IOException;
import java.util.List;

/**
 * @author wangshuqing
 * @date 2023/2/22 11:12
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class EasyExcelExportContext extends AssembleContext {
    /**excelWriter 写入流**/
    private ExcelWriter excelWriter;
    /**当前写入的sheet页**/
    private WriteSheet currSheet;
    /**当前sheet页已经存在的条数**/
    private int currSheetSize;
    /**总sheet 数**/
    protected int totalSheet;


    public WriteSheet createSheet(){
        currSheetSize=0;
        int nowTotalSheet=++this.totalSheet;
        String sheetName=String.format("sheet-%s",nowTotalSheet);
        this.currSheet= EasyExcel.writerSheet(sheetName).build();
        setTotalSheet(nowTotalSheet);
        return currSheet;
    }

    public void writeSheet(List<List<Object>> data){
        excelWriter.write(data,currSheet);
        currSheetSize+=data.size();
        totalSize+=data.size();
    }


    @Override
    public void close() throws IOException {
        if(excelWriter!=null){
            excelWriter.finish();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        EasyExcelExportContext context=new EasyExcelExportContext();
    }

}
